package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.example.library_common.base.LazyFragment
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.CMLIntent

/**
 * CML总资产
 * */
class CMLAllCaptureFragment : LazyFragment() {


    companion object {
        fun newInstance(mCMLIntent: CMLIntent?): CMLAllCaptureFragment {
            val args = Bundle()
            args.putSerializable("mCMLIntent", mCMLIntent)
            val fragment = CMLAllCaptureFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var visibleTv: ImageView

    private lateinit var usdTv: TextView

    private lateinit var cnyTv: TextView

    private lateinit var titleTemplate: TextView

    private var mCMLIntent: CMLIntent? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        mCMLIntent = arguments?.getSerializable("mCMLIntent") as CMLIntent
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun lazyInit() {
        visibleTv.performClick()
    }

    override fun initView(view: View) {
        titleTemplate = view.findViewById(R.id.title_template)
        visibleTv = view.findViewById(R.id.visibleTv)
        usdTv = view.findViewById(R.id.usdTv)
        cnyTv = view.findViewById(R.id.cnyTv)

        visibleTv.setOnClickListener {
            visibleTv.isSelected = !visibleTv.isSelected
            toggleVisible()
        }
        when (mCMLIntent?.transferType) {
            CMLIntent.PARENT_ACCOUNT -> {
                titleTemplate.text = "总资产"
            }
            CMLIntent.MOTHER_ACCOUNT -> {
                titleTemplate.text = "母币总资产"
                val accountType = mCMLIntent?.accountType ?: ""
                val wallet_cml = _mActivity.getString(R.string.wallet_cml)
                if (accountType.contains(wallet_cml)) {
                    cnyTv.visibility = View.GONE
                }
            }
            CMLIntent.CHILD_ACCOUNT -> {
                titleTemplate.text = "子账户总资产"
            }
        }
    }

    private fun toggleVisible() {

        if (visibleTv.isSelected) {
            usdTv.text = mCMLIntent?.cmlBalanceStr()

            cnyTv.text = mCMLIntent?.cnyBalanceStr()
        } else {

            var str0 = ""
            mCMLIntent?.cmlBalanceStr()?.forEach { _ ->
                str0 += "*"
            }
            usdTv.text = str0

            var str1 = ""
            mCMLIntent?.cnyBalanceStr()?.forEach { _ ->
                str1 += "*"
            }
            cnyTv.text = str1
        }
    }


    override fun getLayoutId(): Any {
        return R.layout.cml_all_capture_fragment
    }

    fun showRealBalance(realBalance: String) {
        mCMLIntent?.cmlCount = realBalance
        if (isCallOnViewCreated) {
            toggleVisible()
        }
    }
}